package com.manager.action.admin;

import java.util.List;
import java.util.Map;

import org.common.tool.FillBeanUtils;
import org.common.util.TranslateHelper;
import org.wface.util.ParamInjector;
import org.wface.web.core.ActionSupport;

import com.manager.model.SysLink;
import com.manager.model.SysRole;
import com.manager.service.system.AdminService;
import com.web.tag.bean.PageModel;

public class LinkAction extends ActionSupport {

	private String page;
	private String id;
	private String[] linkCheck;

	public String list() {
		linkListPage(null);
		return "linkList";
	}

	public String edit() {
		AdminService adminService = new AdminService();
		SysLink link = adminService.findLinkById(TranslateHelper.parseInteger(id));
		getRequest().setAttribute("editLink", link);
		// ************************
		linkListPage(adminService);
		return "linkList";
	}

	public String save() {
		SysLink link = new SysLink();
		@SuppressWarnings("unchecked")
		Map<String, String[]> parameterMap = getRequest().getParameterMap();
		new FillBeanUtils(parameterMap).injectBean(link);
		AdminService adminService = new AdminService();
		if (adminService.saveLink(link)) {
			getRequest().setAttribute("saveMsg", "系统Link(Url)保存成功!");
		} else {
			getRequest().setAttribute("saveMsg", "系统Link(Url)保存失败!");
		}
		getRequest().setAttribute("editLink", link);
		// ************************
		linkListPage(adminService);
		return "linkList";
	}

	public String delete() {
		AdminService adminService = new AdminService();
		if (adminService.deleteLink(TranslateHelper.parseInteger(id))) {
			getRequest().setAttribute("saveMsg", "系统Link(Url)删除成功!");
		} else {
			getRequest().setAttribute("saveMsg", "系统Link(Url)删除失败!");
		}
		// *******************
		linkListPage(adminService);
		return "linkList";
	}

	public String roleLink() {
		roleLink(null);
		return "role-link";
	}

	public String roleLinkSave() {
		AdminService adminService = new AdminService();
		if (adminService.saveRoleLink(TranslateHelper.parseInteger(id), linkCheck)) {
			getRequest().setAttribute("saveMsg", "系统角色对应Link(Url)设置成功!");
		} else {
			getRequest().setAttribute("saveMsg", "系统角色对应Link(Url)设置失败!");
		}
		// *******************
		roleLink(adminService);
		return "role-link";
	}

	private void linkListPage(AdminService adminService) {
		PageModel model = new PageModel();
		int pageNum = TranslateHelper.parseInteger(page);
		model.setPageNum(pageNum);
		if (adminService == null)
			adminService = new AdminService();
		List<SysLink> links = adminService.findAllLink(model);
		getRequest().setAttribute("linkList", links);
		model.setPaths(getBasePath() + "/manager/link/list.do?");
		getRequest().setAttribute(PageModel.PAGEMODEL, model);
	}

	private void roleLink(AdminService adminService) {
		if (adminService == null)
			adminService = new AdminService();
		List<SysRole> roles = adminService.findAllRole(null, 1);
		List<SysLink> links = adminService.findAllLink(null, 1);
		getRequest().setAttribute("roleList", roles);
		getRequest().setAttribute("linkList", links);
		SysRole role = adminService.findRoleLink(TranslateHelper.parseInteger(id));
		getRequest().setAttribute("selectRole", role);
	}

	private String getBasePath() {
		String basePath = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + getRequest().getContextPath();
		return basePath;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String[] getLinkCheck() {
		return linkCheck;
	}

	public void setLinkCheck(String[] linkCheck) {
		this.linkCheck = linkCheck;
	}

}
